package with_redis

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
	"time"
)

type Book struct {
	title  string
	author string
}

func change_book(book Book) {
	book.title = "java"
	fmt.Printf("%v\n", &book)
}

func Test1() {
	book := Book{
		title:  "python",
		author: "guidio",
	}
	fmt.Printf("type of book = %T\t", book, "value of book = %v\n", book)
	change_book(book)
	fmt.Printf("type of book = %T\t", book, "value of book = %v\n", book)
}

// 全局上下文
var ctx = context.Background()

func ConnRedis() {
	rdb := redis.NewClient(&redis.Options{
		Addr:         "localhost:6379",
		Password:     "",
		DB:           0,
		PoolSize:     100,
		MinIdleConns: 3,
	})
	err := rdb.Ping(ctx).Err()
	if err != nil {
		fmt.Println("链接失败", err)
		return
	}
	fmt.Println("链接成功 Redis")

	ticker := time.NewTicker(3 * time.Second)

	defer ticker.Stop()

	for range ticker.C {
		if err := rdb.Ping(ctx).Err(); err != nil {
			fmt.Println("链接异常", err)
		} else {
			fmt.Println("链接正常")
		}
	}
}
